package com.danskin.wechat.api.client;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.constant.DanskinConstants;
import com.danskin.wechat.util.FileUploadUtil;
import com.danskin.wechat.util.PropertyUtil;
import com.danskin.wechat.util.StringUtil;

public class WeiXinOpenApiClient extends ApiClient {

	private final static String API_URL = "https://api.weixin.qq.com/";

	private volatile static WeiXinOpenApiClient singleton;

	private WeiXinOpenApiClient() {
	}

	public static WeiXinOpenApiClient createInstance() {
		if (singleton == null) {
			synchronized (WeiXinOpenApiClient.class) {
				if (singleton == null) {
					singleton = new WeiXinOpenApiClient();
				}
			}
		}
		return singleton;
	}

	public JSONObject getAuthorizerToken(String component_access_token, String component_appid, String authorizer_appid,
			String authorizer_refresh_token) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("component_appid", component_appid);
		businessParameters.put("authorizer_appid", authorizer_appid);
		businessParameters.put("authorizer_refresh_token", authorizer_refresh_token);

		String data = businessParameters.toJSONString();

		return sendPost(
				API_URL + "cgi-bin/component/api_authorizer_token?component_access_token=" + component_access_token,
				data);
	}

	/**
	 * 获取微信增减粉丝数
	 * 
	 * @param access_token
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @author makunsmac
	 */
	public String getIncreasedAndLostWxFansCount(String access_token, String beginDate, String endDate) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("begin_date", beginDate);
		businessParameters.put("end_date", endDate);

		String data = businessParameters.toJSONString();

		return sendPostForWx(API_URL + "datacube/getusersummary?access_token=" + access_token, data);
	}

	/**
	 * 获取微信累计粉丝数
	 * 
	 * @param access_token
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @author makunsmac
	 */
	public String getAccumulativeTotalWxFansCount(String access_token, String beginDate, String endDate) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("begin_date", beginDate);
		businessParameters.put("end_date", endDate);

		String data = businessParameters.toJSONString();

		return sendPostForWx(API_URL + "datacube/getusercumulate?access_token=" + access_token, data);
	}

	/**
	 * 获取微信消息发送数
	 * 
	 * @param access_token
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @author makunsmac
	 */
	public String getMsgSendSummaryCount(String access_token, String beginDate, String endDate) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("begin_date", beginDate);
		businessParameters.put("end_date", endDate);

		String data = businessParameters.toJSONString();

		return sendPostForWx(API_URL + "datacube/getupstreammsg?access_token=" + access_token, data);
	}

	/**
	 * 获取消息发送分时数据
	 * 
	 * @param access_token
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @author makunsmac
	 */
	public String getMsgHourCount(String access_token, String beginDate, String endDate) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("begin_date", beginDate);
		businessParameters.put("end_date", endDate);

		String data = businessParameters.toJSONString();

		return sendPostForWx(API_URL + "datacube/getupstreammsghour?access_token=" + access_token, data);
	}

	/**
	 * 获取消息发送周数据
	 * 
	 * @param access_token
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @author makunsmac
	 */
	public String getMsgWeekCount(String access_token, String beginDate, String endDate) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("begin_date", beginDate);
		businessParameters.put("end_date", endDate);

		String data = businessParameters.toJSONString();

		return sendPostForWx(API_URL + "datacube/getupstreammsgweek?access_token=" + access_token, data);
	}

	/**
	 * 获取消息发送月数据
	 * 
	 * @param access_token
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @author makunsmac
	 */
	public String getMsgMonthCount(String access_token, String beginDate, String endDate) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("begin_date", beginDate);
		businessParameters.put("end_date", endDate);

		String data = businessParameters.toJSONString();

		return sendPostForWx(API_URL + "datacube/getupstreammsgmonth?access_token=" + access_token, data);
	}

	public JSONObject getTicket(String access_token, String type) {

		return sendGet(API_URL + "cgi-bin/ticket/getticket?access_token=" + access_token + "&type=" + type, null);
	}

	public JSONObject getOAuth2AccessToken(String component_access_token, String component_appid, String appid,
			String code) {

		Map<String, String> businessParameters = new HashMap<String, String>();
		businessParameters.put("component_access_token", component_access_token);
		businessParameters.put("component_appid", component_appid);
		businessParameters.put("appid", appid);
		businessParameters.put("code", code);
		businessParameters.put("grant_type", "authorization_code");

		return sendGet(API_URL + "sns/oauth2/component/access_token", businessParameters);
	}

	public JSONObject getSnsUserInfo(String access_token, String openId) {

		JSONObject json = sendGet(
				API_URL + "sns/userinfo?access_token=" + access_token + "&openid=" + openId + "&lang=zh_CN", null);

		return json;
	}

	/**
	 * 创建会员卡
	 * 
	 * @param access_token
	 * @param appId
	 * @param logo
	 * @param brandName
	 * @return
	 */
	public JSONObject createMemberCard(String access_token, String appId, String logo, String brandName) {

		JSONObject businessParameters = new JSONObject();

		JSONObject card = new JSONObject();
		card.put("card_type", DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_MEMBER_CARD);

		JSONObject memberCard = new JSONObject();
		memberCard.put("background_pic_url",
				"http://mmbiz.qpic.cn/mmbiz_png/SxpO2L00rVOh1ewxX8hwpXFnicg0dYMtYCXyIBNIUzWNDKK00bVK3z3IJgCcm9V9dWuiafOdweicaHiaImPLiagPlmg/0");

		JSONObject baseInfo = new JSONObject();
		baseInfo.put("logo_url", logo);
		baseInfo.put("brand_name", brandName);
		baseInfo.put("code_type", "CODE_TYPE_BARCODE");
		baseInfo.put("title", brandName + "会员卡");
		baseInfo.put("color", "Color100");
		baseInfo.put("notice", "使用时向服务员出示此券");
		baseInfo.put("description", "会员卡仅限申请者本人使用，会员身份永久有效，会员卡会籍及卡内积分不可转让或出借。\r\n会员结账时请主动提供会员卡券条形码或注册手机号。");
		JSONObject date_info = new JSONObject();
		date_info.put("type", "DATE_TYPE_PERMANENT");
		baseInfo.put("date_info", date_info);
		JSONObject sku = new JSONObject();
		sku.put("quantity", 100000000);
		baseInfo.put("sku", sku);

		baseInfo.put("get_limit", 1);
		baseInfo.put("use_custom_code", false);
		baseInfo.put("can_give_friend", false);
		baseInfo.put("custom_url_name", "会员中心");
		baseInfo.put("custom_url", PropertyUtil.getFrontUrl(appId, "home"));
		baseInfo.put("custom_url_sub_title", "查看专属优惠");
		baseInfo.put("need_push_on_view", true);// 用户在进入会员卡时，微信会把这个事件推送到开发者填写的URL。
		memberCard.put("base_info", baseInfo);
		memberCard.put("supply_bonus", true);
		memberCard.put("bonus_url", PropertyUtil.getFrontUrl(appId, "points"));
		memberCard.put("supply_balance", false);
		memberCard.put("prerogative", this.getPrerogative()); // 特权说明
		memberCard.put("wx_activate", true);

		JSONObject custom_field2 = new JSONObject();
		custom_field2.put("name_type", "FIELD_NAME_TYPE_LEVEL");
		custom_field2.put("name", "会员等级");
		custom_field2.put("url", PropertyUtil.getFrontUrl(appId, "levelcenter"));
		memberCard.put("custom_field2", custom_field2);

		JSONObject custom_field3 = new JSONObject();
		custom_field3.put("name_type", "FIELD_NAME_TYPE_COUPON");
		custom_field3.put("name", "优惠券");
		custom_field3.put("url", PropertyUtil.getFrontUrl(appId, "coupon"));
		memberCard.put("custom_field3", custom_field3);

		JSONObject custom_cell1 = new JSONObject();
		custom_cell1.put("name", "积分商城");
		custom_cell1.put("tips", "前往积分商城");
		custom_cell1.put("url", PropertyUtil.getFrontUrl(appId, "mall"));
		memberCard.put("custom_cell1", custom_cell1);

		card.put("member_card", memberCard);
		businessParameters.put("card", card);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/create?access_token=" + access_token, data);
	}

	/**
	 * 创建现金券
	 * 
	 * @param access_token
	 * @param appId
	 * @param logo
	 * @param brandName
	 * @return
	 */
	public JSONObject createCashCoupon(String access_token, String appId, String logo, String brandName, String title,
			String description, long beginTime, long endTime, BigDecimal leastCost, BigDecimal reduceCost) {

		JSONObject businessParameters = new JSONObject();

		JSONObject card = new JSONObject();
		card.put("card_type", DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_CASH);

		JSONObject memberCard = new JSONObject();
		JSONObject baseInfo = new JSONObject();
		baseInfo.put("logo_url", logo);
		baseInfo.put("brand_name", brandName);
		baseInfo.put("code_type", "CODE_TYPE_TEXT");
		baseInfo.put("title", title);
		baseInfo.put("color", "Color100");
		baseInfo.put("notice", "仅限门店使用");
		baseInfo.put("service_phone", "400-610-0011");
		baseInfo.put("description", description);
		JSONObject date_info = new JSONObject();
		date_info.put("type", "DATE_TYPE_FIX_TIME_RANGE");
		date_info.put("begin_timestamp", beginTime);
		date_info.put("end_timestamp", endTime);
		baseInfo.put("date_info", date_info);
		JSONObject sku = new JSONObject();
		sku.put("quantity", 100000000);
		baseInfo.put("sku", sku);

		// baseInfo.put("get_limit", 1);
		baseInfo.put("use_custom_code", true);
		baseInfo.put("bind_openid", false);
		baseInfo.put("can_share", false);
		baseInfo.put("can_give_friend", false);
		baseInfo.put("center_title", "卡券中心");
		baseInfo.put("center_sub_title", "满一定金额可用");
		baseInfo.put("center_url", PropertyUtil.getFrontUrl(appId, "coupon"));
		baseInfo.put("custom_url_name", "会员中心");
		baseInfo.put("custom_url", PropertyUtil.getFrontUrl(appId, "home"));
		baseInfo.put("custom_url_sub_title", "查看会员卡");
		baseInfo.put("promotion_url_name", "积分商城");
		baseInfo.put("promotion_url", PropertyUtil.getFrontUrl(appId, "mall"));
		baseInfo.put("source", brandName);

		memberCard.put("base_info", baseInfo);
		memberCard.put("least_cost", leastCost.multiply(new BigDecimal(100)));
		memberCard.put("reduce_cost", reduceCost.multiply(new BigDecimal(100)));

		card.put("cash", memberCard);
		businessParameters.put("card", card);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/create?access_token=" + access_token, data);
	}

	/**
	 * 创建折扣券
	 * 
	 * @param access_token
	 * @param appId
	 * @param logo
	 * @param brandName
	 * @return
	 */
	public JSONObject createDiscountCoupon(String access_token, String appId, String logo, String brandName,
			String title, String description, long beginTime, long endTime, BigDecimal discount) {

		JSONObject businessParameters = new JSONObject();

		JSONObject card = new JSONObject();
		card.put("card_type", DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_DISCOUNT);

		JSONObject memberCard = new JSONObject();
		JSONObject baseInfo = new JSONObject();
		baseInfo.put("logo_url", logo);
		baseInfo.put("brand_name", brandName);
		baseInfo.put("code_type", "CODE_TYPE_TEXT");
		baseInfo.put("title", title);
		baseInfo.put("color", "Color100");
		baseInfo.put("notice", "仅限门店使用");
		baseInfo.put("service_phone", "400-610-0011");
		baseInfo.put("description", description);
		JSONObject date_info = new JSONObject();
		date_info.put("type", "DATE_TYPE_FIX_TIME_RANGE");
		date_info.put("begin_timestamp", beginTime);
		date_info.put("end_timestamp", endTime);
		baseInfo.put("date_info", date_info);
		JSONObject sku = new JSONObject();
		sku.put("quantity", 100000000);
		baseInfo.put("sku", sku);

		baseInfo.put("use_custom_code", true);
		baseInfo.put("bind_openid", false);
		baseInfo.put("can_share", false);
		baseInfo.put("can_give_friend", false);
		baseInfo.put("center_title", "更多卡券");
		baseInfo.put("center_sub_title", "满一定折扣可用");
		baseInfo.put("center_url", PropertyUtil.getFrontUrl(appId, "coupon"));
		baseInfo.put("custom_url_name", "会员中心");
		baseInfo.put("custom_url", PropertyUtil.getFrontUrl(appId, "home"));
		baseInfo.put("custom_url_sub_title", "查看会员卡");
		baseInfo.put("promotion_url_name", "积分商城");
		baseInfo.put("promotion_url", PropertyUtil.getFrontUrl(appId, "mall"));
		baseInfo.put("source", brandName);

		memberCard.put("base_info", baseInfo);
		memberCard.put("discount", new BigDecimal(1).subtract(discount).multiply(new BigDecimal(100)));

		card.put("discount", memberCard);
		businessParameters.put("card", card);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/create?access_token=" + access_token, data);
	}

	private String getPrerogative() {
		// String str =
		// "新锐会员：\r\n入会条件：注册并完善个信息即可入会；\r\n门店特权：O2O门店消费免邮；\r\n官网生日礼券：20元代金券\r\n\r\n菁英会员：\r\n入会条件：一年内累计消费满380元\r\n门店特权：正价商品享9.8折优惠、O2O门店消费免邮；\r\n官网会员礼券：免邮券\r\n官网生日礼券：30元代金券\r\n官网生日礼品：随单礼品\r\n\r\n铜牌会员：\r\n入会条件：一次性购物满500元或一年内累计消费满980元\r\n门店会员礼券：50元代金券一张\r\n门店生日礼券：生日6折券一张\r\n门店特权：正价商品享9.5折优惠、O2O门店消费免邮\r\n官网会员礼券：50元代金券一张\r\n官网生日礼券：50元代金券\r\n官网生日礼品：随单礼品\r\n\r\n银牌会员：\r\n入会条件：一年内累计消费满1580元\r\n门店会员礼券：50元代金券两张、6折券一张\r\n门店生日礼券：生日6折券一张\r\n门店特权：正价商品享9.2折优惠、O2O门店消费免邮\r\n官网会员礼券：50元代金券两张\r\n官网生日礼券：80元代金券\r\n官网生日礼品：随单礼品\r\n\r\n金牌会员：\r\n入会条件：一年内累计消费满2580元\r\n门店会员礼券：50元代金券四张、6折券两张\r\n门店生日礼券：生日6折券一张\r\n门店特权：正价商品享8.8折优惠、O2O门店消费免邮\r\n官网会员礼券：50元代金券四张\r\n官网生日礼券：100元代金券\r\n官网生日礼品：随单礼品\r\n\r\n韦德会员：\r\n入会条件：当年一次性购物满2588元（限韦德专营店满额消费）\r\n门店会员礼券：50元代金券四张、6折券两张（限李宁店）\r\n门店生日礼券：生日当天韦德门店享6折券三张（特例品除外）\r\n门店特权：正价商品享8.8折优惠（签名款除外)、O2O门店消费免邮\r\n官网会员礼券：50元代金券四张\r\n官网生日礼券：100元代金券\r\n官网生日礼品：随单礼品\r\n\r\n1、入会渠道\r\n在李宁宁聚卡店铺登记、李宁官方网站注册、关注李宁体育微信申领电子卡，均可成为会员，每名会员只可办理一张会员卡。\r\n2、升级规则\r\n会员一年内累计消费金额达到相应等级条件即可自动升级。\r\n店铺范围：会员在李宁宁聚卡认证店铺以及李宁官方网店的消费均可计入累计消费额。\r\n累计消费金额：指的是实际支付金额而非订单金额，不包括运费、退货费、优惠券抵用金额以及相关让利折扣等。\r\n等级生效时间：会员在宁聚卡认证店铺的消费额将于成交次日计入累计消费额，参与计算升级；会员在李宁官方网店的消费额将于成交后的14天计入累计消费额，参与计算升级。\r\n3、关于一次性消费满500元条款\r\n作为晋级激励政策，仅当等级低于铜牌时，一次性消费满500元，可以直接升级至铜牌；\r\n升级至铜牌后的单笔500元订单仅按照累计消费额计算，不作为重新计算铜牌有效期的依据；\r\n4、等级有效期：\r\n会员等级自升级之日起保持1年，到期后按照1年内的累计消费额重新计算会员等级，新等级从当前等级到期日次日开始生效。\r\n注：当计算保降级规则时，统一按照累计消费额计算，无一次性消费满500元的激励政策。";
		String str = "新锐会员：\r\n入会条件：注册并完善个信息即可入会；\r\n门店特权：O2O门店消费免邮；\r\n官网生日礼券：20元代金券\r\n\r\n菁英会员：\r\n入会条件：一年内累计消费满380元\r\n门店特权：正价商品享9.8折优惠、O2O门店消费免邮；\r\n官网会员礼券：免邮券\r\n官网生日礼券：30元代金券\r\n官网生日礼品：随单礼品\r\n\r\n铜牌会员：\r\n入会条件：一次性购物满500元或一年内累计消费满980元\r\n门店会员礼券：50元代金券一张\r\n门店生日礼券：生日6折券一张\r\n门店特权：正价商品享9.5折优惠、O2O门店消费免邮\r\n官网会员礼券：50元代金券一张\r\n官网生日礼券：50元代金券\r\n官网生日礼品：随单礼品\r\n\r\n银牌会员：\r\n入会条件：一年内累计消费满1580元\r\n门店会员礼券：50元代金券两张、6折券一张\r\n门店生日礼券：生日6折券一张\r\n门店特权：正价商品享9.2折优惠、O2O门店消费免邮\r\n官网会员礼券：50元代金券两张\r\n官网生日礼券：80元代金券\r\n官网生日礼品：随单礼品\r\n\r\n金牌会员：\r\n入会条件：一年内累计消费满2580元\r\n门店会员礼券：50元代金券四张、6折券两张\r\n门店生日礼券：生日6折券一张\r\n门店特权：正价商品享8.8折优惠、O2O门店消费免邮\r\n官网会员礼券：50元代金券四张\r\n官网生日礼券：100元代金券\r\n官网生日礼品：随单礼品\r\n\r\n韦德会员：\r\n入会条件：当年一次性购物满2588元（限韦德专营店满额消费）\r\n门店会员礼券：50元代金券四张、6折券两张（限李宁店）\r\n门店生日礼券：生日当天韦德门店享6折券三张（特例品除外）\r\n门店特权：正价商品享8.8折优惠（签名款除外)、O2O门店消费免邮\r\n官网会员礼券：50元代金券四张\r\n官网生日礼券：100元代金券\r\n官网生日礼品：随单礼品\r\n\r\n";
		return str;
	}

	/**
	 * 设置开卡字段接口
	 * 
	 * @param access_token
	 * @param appId
	 * @param cardId
	 * @return
	 */
	public JSONObject activateUserForm(String access_token, String appId, String cardId) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("card_id", cardId);

		JSONObject required_form = new JSONObject();
		required_form.put("can_modify", false);
		required_form.put("common_field_id_list", new String[] { "USER_FORM_INFO_FLAG_MOBILE" });
		businessParameters.put("required_form", required_form);

		JSONObject service_statement = new JSONObject();
		service_statement.put("name", "会员权益");
		service_statement.put("url", PropertyUtil.getFrontUrl(appId, "leveldetai"));
		businessParameters.put("service_statement", service_statement);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/membercard/activateuserform/set?access_token=" + access_token, data);
	}

	/**
	 * 激活会员卡
	 * 
	 * @param access_token
	 * @param cardId
	 * @param code
	 * @return
	 */
	public JSONObject activateMemberCard(String access_token, String cardId, String code, String mobile, Integer points,
			String vipLevel, Integer couponNum) {
		JSONObject businessParameters = new JSONObject();
		businessParameters.put("card_id", cardId);
		businessParameters.put("membership_number", mobile);
		businessParameters.put("code", code);
		businessParameters.put("init_custom_field_value1", "积分");
		if (vipLevel != null) {
			businessParameters.put("init_custom_field_value2", vipLevel);
		}
		if (couponNum != null) {
			businessParameters.put("init_custom_field_value3", couponNum);
		}
		if (points != null) {
			businessParameters.put("init_bonus", points);
		}
		businessParameters.put("init_balance", 0);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/membercard/activate?access_token=" + access_token, data);
	}

	/**
	 * 更新会员信息
	 * 
	 * @param access_token
	 * @param cardId
	 * @param code
	 * @param bonus
	 * @param addBonus
	 * @param vipLevel
	 * @param couponNum
	 * @return
	 */
	public JSONObject updateUser(String access_token, String cardId, String code, int bonus, String vipLevel,
			int couponNum) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("card_id", cardId);
		businessParameters.put("code", code);
		// businessParameters.put("custom_field_value1", bonus);
		businessParameters.put("bonus", bonus);
		businessParameters.put("custom_field_value2", vipLevel);// 会员等级
		businessParameters.put("custom_field_value3", couponNum); // 优惠券数量

		JSONObject notify_optional = new JSONObject();
		notify_optional.put("is_notify_bonus", false);// 积分变动时是否触发系统模板消息，默认为true
		notify_optional.put("is_notify_balance", false);// 余额变动时是否触发系统模板消息，默认为true
		businessParameters.put("notify_optional", notify_optional);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/membercard/updateuser?access_token=" + access_token, data);
	}

	/**
	 * 获取粉丝会员卡信息
	 * 
	 * @param access_token
	 * @param cardId
	 * @param code
	 * @return
	 * @author likui.liu
	 */
	public JSONObject getMemberCardUserInfo(String access_token, String cardId, String code) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("card_id", cardId);
		businessParameters.put("code", code);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "card/membercard/userinfo/get?access_token=" + access_token, data);
	}

	public JSONObject getMaterialBatch(String access_token, String type, int offset, int count) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("type", type);
		businessParameters.put("offset", offset);
		businessParameters.put("count", count);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "cgi-bin/material/batchget_material?access_token=" + access_token, data);
	}

	public JSONObject getMaterial(String access_token, String mediaId) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("media_id", mediaId);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "cgi-bin/material/get_material?access_token=" + access_token, data);
	}

	public JSONObject addMaterial(String access_token, String type, String fileContentType, File file,
			Map<String, String> form) {

		Map<String, String> queryParas = new HashMap<String, String>();
		queryParas.put("access_token", access_token);
		queryParas.put("type", type);// 媒体文件类型，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）

		String result = FileUploadUtil.imageUpload(API_URL + "cgi-bin/material/add_material", file, queryParas, form,
				"media", fileContentType);

		return JSON.parseObject(result);
	}

	public JSONObject createMenu(String access_token, JSONArray buttons) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("button", buttons);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "cgi-bin/menu/create?access_token=" + access_token, data);
	}

	/**
	 * 发送客服消息：文本消息
	 * 
	 * @param access_token
	 * @param openId
	 * @param content
	 * @return
	 */
	public JSONObject sendKfTextMessage(String access_token, String openId, String content) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("touser", openId);
		businessParameters.put("msgtype", "text");
		JSONObject jsonContent = new JSONObject();
		jsonContent.put("content", content);
		businessParameters.put("text", jsonContent);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "cgi-bin/message/custom/send?access_token=" + access_token, data);
	}

	public JSONObject getFansList(String access_token, String nextOpenId) {
		String url = API_URL + "cgi-bin/user/get?access_token=" + access_token;
		if (nextOpenId != null && !nextOpenId.equals("")) {
			url = url + "&next_openid=" + nextOpenId;
		}
		return sendGet(url, null);
	}

	public JSONObject getFansInfo(String access_token, String openId) {

		return sendGet(API_URL + "cgi-bin/user/info?access_token=" + access_token + "&openid=" + openId + "&lang=zh_CN",
				null);
	}

	public JSONObject createQrcode(String access_token, int limitType, int expireSeconds, String sceneStr) {

		JSONObject businessParameters = new JSONObject();
		if (limitType == 0) {// 临时二维码
			businessParameters.put("expire_seconds", expireSeconds);
			businessParameters.put("action_name", "QR_STR_SCENE");
		} else if (limitType == 1) {// 永久二维码
			businessParameters.put("action_name", "QR_LIMIT_STR_SCENE");
		}

		JSONObject scene = new JSONObject();
		scene.put("scene_str", sceneStr);
		JSONObject action_info = new JSONObject();
		action_info.put("scene", scene);
		businessParameters.put("action_info", action_info);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "cgi-bin/qrcode/create?access_token=" + access_token, data);
	}

	/**
	 * 发送模版消息
	 * 
	 * @param access_token
	 * @param openId
	 * @param templateId
	 * @param url
	 * @param json
	 * @return
	 */
	public JSONObject sendMsgTemplate(String access_token, String openId, String templateId, String url,
			JSONObject json) {

		JSONObject businessParameters = new JSONObject();
		businessParameters.put("touser", openId);
		businessParameters.put("template_id", templateId);
		if (!StringUtil.isNullOrEmpty(url)) {
			businessParameters.put("url", url);
		}
		businessParameters.put("data", json);

		String data = businessParameters.toJSONString();
		return sendPost(API_URL + "cgi-bin/message/template/send?access_token=" + access_token, data);
	}

	public static void main(String[] args) {
		// WeiXinOpenApiClient.createInstance().createCard(
		// "vJDtkNxiQBZQ0zIk_Oe3hswL8fum0n4wSAOWytouaERpPN5IjjTxG1BdWvxzbTQTXCCDMdt89-zVZuk8Vt0IWij982mltSVQWe9D9Twpc_2cAXbVHMRb9R89HBJzV0aHWJXbAGDGTH",
		// "wxd3150fe826ac186f",
		// "http://wx.qlogo.cn/mmopen/3rkAFJ6w75YHmcawgbjr1QezCFUBthe3UljOSEEM54qevXG07WZYl1gHu9icia5G332cv6jyu1LTicsbibO9JKSxWvZoD9saSgDo/0",
		// "WELINK");

		// WeiXinOpenApiClient.createInstance().activateUserForm(
		// "f1WWTkea6msareeFIAIAtS6kqWz4mpMzaNla6mpLIMHgKm9aKRcmisSCGFgXU9GRUOGi2nXL_cLhcp1j20GaF_gGm-Od9Sub1xv4kNWLD9ll7yqvEg--0QKi1vEBXD2tZHGaADDNVU",
		// "wxd3150fe826ac186f", "p02XP1ZP5tMcIovxZ-4tTv26QSNw");
	}
}
